package com.ruoyi.system.service.impl;

import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import com.ruoyi.common.utils.StringUtils;
import org.springframework.transaction.annotation.Transactional;
import com.ruoyi.system.domain.WorkPaperWinners;
import com.ruoyi.system.mapper.WorkPaperMapper;
import com.ruoyi.system.domain.WorkPaper;
import com.ruoyi.system.service.IWorkPaperService;

/**
 * 论文Service业务层处理
 * 
 * @author ruoyi
 * @date 2024-07-10
 */
@Service
public class WorkPaperServiceImpl implements IWorkPaperService 
{
    @Autowired
    private WorkPaperMapper workPaperMapper;

    /**
     * 查询论文
     * 
     * @param id 论文主键
     * @return 论文
     */
    @Override
    public WorkPaper selectWorkPaperById(Long id)
    {
        return workPaperMapper.selectWorkPaperById(id);
    }

    /**
     * 查询论文列表
     * 
     * @param workPaper 论文
     * @return 论文
     */
    @Override
    public List<WorkPaper> selectWorkPaperList(WorkPaper workPaper)
    {
        return workPaperMapper.selectWorkPaperList(workPaper);
    }

    /**
     * 新增论文
     * 
     * @param workPaper 论文
     * @return 结果
     */
    @Transactional
    @Override
    public int insertWorkPaper(WorkPaper workPaper)
    {
        int rows = workPaperMapper.insertWorkPaper(workPaper);
        insertWorkPaperWinners(workPaper);
        return rows;
    }

    /**
     * 修改论文
     * 
     * @param workPaper 论文
     * @return 结果
     */
    @Transactional
    @Override
    public int updateWorkPaper(WorkPaper workPaper)
    {
        workPaperMapper.deleteWorkPaperWinnersByPaperId(workPaper.getId());
        insertWorkPaperWinners(workPaper);
        return workPaperMapper.updateWorkPaper(workPaper);
    }

    /**
     * 批量删除论文
     * 
     * @param ids 需要删除的论文主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteWorkPaperByIds(Long[] ids)
    {
        workPaperMapper.deleteWorkPaperWinnersByPaperIds(ids);
        return workPaperMapper.deleteWorkPaperByIds(ids);
    }

    /**
     * 删除论文信息
     * 
     * @param id 论文主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteWorkPaperById(Long id)
    {
        workPaperMapper.deleteWorkPaperWinnersByPaperId(id);
        return workPaperMapper.deleteWorkPaperById(id);
    }

    /**
     * 新增论文完成人信息
     * 
     * @param workPaper 论文对象
     */
    public void insertWorkPaperWinners(WorkPaper workPaper)
    {
        List<WorkPaperWinners> workPaperWinnersList = workPaper.getWorkPaperWinnersList();
        Long id = workPaper.getId();
        if (StringUtils.isNotNull(workPaperWinnersList))
        {
            List<WorkPaperWinners> list = new ArrayList<WorkPaperWinners>();
            for (WorkPaperWinners workPaperWinners : workPaperWinnersList)
            {
                workPaperWinners.setPaperId(id);
                list.add(workPaperWinners);
            }
            if (list.size() > 0)
            {
                workPaperMapper.batchWorkPaperWinners(list);
            }
        }
    }
}
